﻿using System;
using System.Linq;
using PSE.Deployment.Engine;
using PSE.Deployment.Engine.Enums;
using PSE.Deployment.Engine.Interfaces;
using PSE.Deployment.SetupBase.Parameter.Enums;

namespace Sinacor.Deployment.Setup.Wizard.Steps
{
    public class Prerequisite : BaseStep
    {
        public StepNavigationAction NavigationAction { get; protected set; }
        private UI.PreRequisites prerequisites;

        public Prerequisite(IStepGroup parent)
            : base(parent)
        {
            this.Id = "Prerequisite";
            this.ExecutionFinished += new EventHandler<PSE.Deployment.Engine.CustomEventArgs.ExecutionFinishedEventArgs>(Prerequisite_ExecutionFinished);
            this.ErrorBehavior = PSE.Deployment.Engine.Enums.StepErrorBehavior.NotifyParent;
            this.NavigationBehavior = PSE.Deployment.Engine.Enums.StepNavigationBehavior.NextPreviousCancel;
        }

        void Prerequisite_ExecutionFinished(object sender, PSE.Deployment.Engine.CustomEventArgs.ExecutionFinishedEventArgs e)
        {
            e.NavigationAction = this.NavigationAction;
        }

        public override void Execute()
        {
            prerequisites = new UI.PreRequisites();
            prerequisites.ShowDialog();
            this.NavigationAction = prerequisites.NavigationAction.GetValueOrDefault();
        }

        public override void Undo()
        {
            if (prerequisites != null)
                prerequisites.Close(false);
        }
    }
}
